package com.jzo2o.customer.controller.consumer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api("地址薄管理")
public class AddressBookController {
    @Resource
    private IAddressBookService addressBookService;

    @PostMapping
    @ApiOperation("新增地址薄接口")
    public void addAddressBook(@RequestBody @ApiParam(name = "dto", value = "地址薄信息")
                               AddressBookUpsertReqDTO dto, HttpServletRequest request) {
        addressBookService.addAddressBook(dto, request);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询地址薄接口")
    public PageResult<AddressBook> getPageAddress(@ApiParam(name = "dto", value = "分页查询参数")
                                            AddressBookPageQueryReqDTO dto) {
        return addressBookService.getPageAddress(dto);
    }

    @GetMapping("/{id}")
    @ApiOperation("地址薄数据回显接口")
    public AddressBook getAddressBook(@PathVariable("id") @ApiParam(name = "id", value = "id")
                                            Long id) {
        return addressBookService.getById(id);
    }

    @PutMapping("/{id}")
    @ApiOperation("编辑地址薄接口")
    public void updateAddressBook(
            @RequestBody @ApiParam(name = "dto", value = "地址薄数据")
            AddressBookUpsertReqDTO dto,
            @PathVariable("id") @ApiParam(name = "id", value = "id")
            Long id
    ) {
        addressBookService.updateAddressBook(id, dto);
    }

    @DeleteMapping("/batch")
    @ApiOperation("批量删除地址薄接口")
    public void deleteAddressBook(@RequestBody @ApiParam(name = "ids", value = "id集合")
                                  List<String> ids) {
        if(ids.size() == 0) {
            throw new ForbiddenOperationException("请选择要删除的地址薄");
        }
        addressBookService.removeByIds(ids);
    }

    @PutMapping("/default")
    @ApiOperation("设置/取消默认地址接口")
    public void yNDefault(
            @RequestParam @ApiParam(name = "id", value = "id")
            Long id,
            @RequestParam @ApiParam(name = "flag", value = "是否默认")
            Integer flag
    ) {
        addressBookService.yNDefault(id, flag);
    }

    @GetMapping("/defaultAddress")
    @ApiOperation("获取默认地址接口")
    public AddressBook getDefaultAddress() {
        return addressBookService.getOne(
                new LambdaQueryWrapper<AddressBook>()
                        .eq(AddressBook::getIsDefault, 1)
        );
    }
}
